<?php 
defined('IN_TS') or die('Access Denied.');

//DES加密函数==================================
function DESencrypt($str) {
	//加密，返回大写十六进制字符串
	$size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
	$str = pkcs5Pad ( $str, $size );
	return strtoupper( bin2hex( @mcrypt_cbc(MCRYPT_DES, 'D2440C13', $str, MCRYPT_ENCRYPT, 0 ) ) );
}



function DESdecrypt($str) {
//解密
	$strBin = hex2bin( strtolower( $str ) );
	$str = @mcrypt_cbc( MCRYPT_DES, 'D2440C13', $strBin, MCRYPT_DECRYPT, 0 );
	$str = pkcs5Unpad( $str );
	return $str;
}


/*
function hex2bin($hexData) {
	$binData = "";
	for($i = 0; $i < strlen ( $hexData ); $i += 2) {
		$binData .= chr ( hexdec ( substr ( $hexData, $i, 2 ) ) );
	}
	return $binData;
}
*/

function pkcs5Pad($text, $blocksize) {
	$pad = $blocksize - (strlen ( $text ) % $blocksize);
	return $text . str_repeat ( chr ( $pad ), $pad );
}

function pkcs5Unpad($text) {
	$pad = ord ( $text {strlen ( $text ) - 1} );
	if ($pad > strlen ( $text ))
		return false;
	if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)
		return false;
	return substr ( $text, 0, - 1 * $pad );
}
//DES加密函数end==================================